import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:video_player/video_player.dart';
import 'package:xvideos/assets_tool/images.dart';
import 'package:xvideos/hj_model/video_model.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/routers/jump_router.dart';

import '../../../tools_base/global_store/store.dart';
import '../../main_page/provider/msg_provider.dart';
import '../../pre_sale/limit_time_event/limit_time_event_provider.dart';

class NewerTimerAcView extends StatefulWidget {
  final VideoPlayerController? videoPlayerController;
  final VideoModel? videoModel;
  final EdgeInsetsGeometry? margin;
  final bool isFromHome;

  const NewerTimerAcView({
    Key? key,
    this.videoPlayerController,
    this.videoModel,
    this.margin,
    this.isFromHome = false,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _NewerTimerAcView();
  }
}

class _NewerTimerAcView extends State<NewerTimerAcView> {
  @override
  Widget build(BuildContext context) {
    return Consumer<MineMsgProvider>(
      builder: (context, newser, child) => Consumer<LimitTimeEventProvider>(
        builder: (context, limitTime, child) {
          {
            //2.存在限时活动，显示限时活动
            if (newser.timeEnd && limitTime.showLimitEvent()) return limitTime.limitBannerDetailView();
            //1.非会员 && 新人倒计时，显示新人倒计时
            if (!globalStore.isVIP()) return _buildBeginnerView(newser);
            //3.会员 无任何展示
            return SizedBox();
          }
        },
      ),
    );
  }

  //新人会员倒计时
  _buildBeginnerView(MineMsgProvider provider) {
    return InkWell(
      onTap: () async {
        widget.videoPlayerController?.pause();
        await jumpToWalletPage();
        await globalStore.updateUserInfo();
        setState(() {});
      },
      child: Container(
        height: 38,
        margin: widget.margin,
        decoration: BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage("count_down_demon_bg.webp".videoPath),
          ),
        ),
        child: Row(
          children: [
            if (provider.timeEnd) ...[
              100.sizeBoxW,
              Text(
                '开通会员 享专属特权',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 11,
                  fontWeight: FontWeight.w600,
                ),
              ),
              Spacer(),
            ] else ...[
              100.sizeBoxW,
              Text(
                '新用户升级会员特惠',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 11,
                  fontWeight: FontWeight.w600,
                ),
              ),
              SizedBox(width: 6),
              Text(
                '${provider.countdown?.hourDesc ?? '00'}:${provider.countdown?.minDesc ?? '00'}:${provider.countdown?.secondsDesc ?? '00'}',
                style: TextStyle(color: Colors.white, fontSize: 11, fontWeight: FontWeight.w600),
              ),
              Spacer(),
            ],
            SizedBox(width: 6),
            InkWell(
              onTap: () async {
                widget.videoPlayerController?.pause();
                await jumpToWalletPage();
                await globalStore.updateUserInfo();
                setState(() {});
              },
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                child: Row(
                  children: [
                    Text(
                      "升级会员",
                      style: TextStyle(
                        height: 20 / 14,
                        fontSize: 12,
                        color: Color(0xffE3B284),
                      ),
                    ),
                    1.sizeBoxW,
                    Image.asset("arrow_yellow.webp".videoPath, width: 14),
                  ],
                ),
              ),
            ),
            SizedBox(width: 6),
          ],
        ),
      ),
    );
  }

  //新人会员倒计时
  _buildNormalView(MineMsgProvider provider) {
    return InkWell(
      onTap: () async {
        widget.videoPlayerController?.pause();
        await jumpToWalletPage();
        await globalStore.updateUserInfo();
        setState(() {});
      },
      child: Container(
        height: 38,
        margin: widget.margin,
        decoration: BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              "count_down_finish_bg.webp".videoPath,
            ),
          ),
        ),
        child: Row(
          children: [
            Spacer(),
            Text('开通VIP 免费观看百万会员影片', style: TextStyle(color: Colors.white, fontSize: 12)),
            12.sizeBoxW,
            SizedBox(width: 6),
            InkWell(
              onTap: () async {
                widget.videoPlayerController?.pause();
                await jumpToWalletPage();
                await globalStore.updateUserInfo();
                setState(() {});
              },
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 4),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  gradient: LinearGradient(
                    colors: [Color(0xffFDE5D0), Color(0xffE7B88F)],
                  ),
                ),
                child: Text(
                  "立即开通",
                  style: TextStyle(
                    height: 20 / 14,
                    fontSize: 12,
                    color: Color(0xff0E141E),
                  ),
                ),
              ),
            ),
            SizedBox(width: 6),
          ],
        ),
      ),
    );
  }
}
